package org.ecnu.java.test;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;


import javax.imageio.ImageIO;


public class Printing {
	public Printing() {
	}

	public static void paint(String inputFileName, String outputFileName,
			int width, int height) throws Exception {
		BufferedImage bi = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);

		Graphics g = bi.getGraphics();
		g.clearRect(0, 0, width, height);

		String fileName = (String) inputFileName.substring(
				inputFileName.lastIndexOf("\\") + 1).replace(".img", "");

		int noise = 0, LimRayleigh = 10000,Lim_min = 0,Lim_max = 0;
		float K = 0.0f, I;

		if (fileName.charAt(0) == 'E' && fileName.charAt(1) == 'N') {
			if (fileName.charAt(8) == 'R') {
				noise = 1136;
				LimRayleigh = 2000;
				Lim_min = 1101;
				Lim_max = 5011;
				K = 1.2037f;
			} else if (fileName.charAt(8) == 'G') {
				noise = 562;
				LimRayleigh = 2000;
				Lim_min = 250;
				Lim_max = 3500;
				K = 2.5372f;
			} else if (fileName.charAt(8) == 'V') {
				noise = 590;
				LimRayleigh = 2000;
				Lim_min = 550;
				Lim_max = 1530;
				K = 3.5652f;
			}
		} else if (fileName.charAt(0) == 'N') {
			if (fileName.charAt(7) == 'R') {
				noise = 1137;
				LimRayleigh = 2000;
				Lim_min = 0;
				Lim_max = 7000;
				K = 0.5159f;
			} else if (fileName.charAt(7) == 'G') {
				noise = 564;
				LimRayleigh = 2000;
				Lim_min = 0;
				Lim_max = 5000;
				K = 1.0909f;
			} else if (fileName.charAt(7) == 'V') {
				noise = 594;
				LimRayleigh = 2000;
				Lim_min = 0;
				Lim_max = 2000;
				K = 1.5280f;
			}
		}

		int result[][] = DataRead.GetData(inputFileName);
		int[][] buffer = new int[512][512];
		for (int i = 0; i < 512; i++) {
			for (int j = 0; j < 512; j++) {
				buffer[i][j] = result[j][i];
			}
		}
		for (int i = 0; i < 512; i++) {
			for (int j = 0; j < 512; j++) {
				int p = buffer[i][j];
				I = (p - noise) * K;

				I = (I/LimRayleigh);
				if(I<0)
					I=0;
				if(I>1)
					I=1;					
				g.setColor(new Color(I, I, I));
				g.drawLine(i, j, i, j);
			}
		}

		File file = new File(outputFileName);
		if (!file.exists())
			file.createNewFile();
		ImageIO.write(bi, "jpg", file);
	}

	public static void main(String[] args)throws Exception{
		
		String infile  ="e:\\N20031223G062731.img";
		String outfile ="e:\\N20031223G062731.jpg";
		paint(infile,outfile,512,512);
		
	}
}